Page({
  data: {
    currentDate: new Date().getTime(),
    // 控制报修房屋的显示隐藏
    houseLayerVisible: false,
    // 控制维修项目的显示隐藏
    repairLayerVisible: false,
    // 控制预约日期的显示隐藏
    dateLayerVisible: false,
    houseList: [],
    repairItem: [],
    houseId: '',
    repairItemId: '',
    mobile: '',
    appointment: '',
    description: '',
    attachment: [],
  },
  openHouseLayer() {
    this.setData({
      houseLayerVisible: true
    })
  },
  closeHouseLayer() {
    this.setData({
      houseLayerVisible: false
    })
  },
  openRepairLayer() {
    this.setData({
      repairLayerVisible: true
    })
  },
  closeRepairLayer() {
    this.setData({
      repairLayerVisible: false,
    })
  },

  openDateLayer() {
    this.setData({
      dateLayerVisible: true
    })
  },
  closeDateLayer() {
    this.setData({
      dateLayerVisible: false
    })
  },
  goList() {
    wx.reLaunch({
      url: '/repair_pkg/pages/list/index',
    })
  },
  selectHouse(ev) {
    this.setData({
      houseInfo: ev.detail.name,
      houseId: ev.detail.id
    })
  },
  selectRepairItem(ev) {
    this.setData({
      repairItemName: ev.detail.name,
      repairItemId: ev.detail.id
    })
  },
  selectDate(ev) {
    const r =
      console.log(r)
    this.setData({
      dateLayerVisible: false,
      appointment: wx.utils.formatDate(ev.detail)
    })
  },
  onLoad({
    id
  }) {
    // 保修房屋，必须是审核通过的才能看到
    this.getHouseList()
    // 维修项目
    this.getRepairItem()
    if (id) {
      wx.setNavigationBarTitle({
        title: '修改报修信息',
      })
      // 根据 id 获取详情并填充
      this.getRepairDetailById(id)
    }
  },
  async getRepairDetailById(id) {
    const {
      code,
      data
    } = await wx.http.get(`/repair/${id}`)
    if (code !== 10000) return wx.utils.toast('获取详情失败')
    this.setData(data)
  },
  async getHouseList() {
    const {
      code,
      data: houseList
    } = await wx.http.get('/house')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      houseList
    })
  },
  async getRepairItem() {
    const {
      code,
      data: repairItem
    } = await wx.http.get('/repairItem')
    if (code !== 10000) return wx.utils.toast()
    this.setData({
      repairItem
    })
  },
  afterRead(e) {
    const filePath = e.detail.file.tempFilePath

    wx.uploadFile({
      url: wx.http.baseURL + '/upload',
      filePath,
      name: 'file',
      header: {
        Authorization: getApp().token
      },
      formData: {
        type: 'avatar'
      },
      success: res => {
        const data = JSON.parse(res.data)
        this.data.attachment.push(data.data)
        this.setData({
          attachment: this.data.attachment
        })
      }
    })
  },
  verifyHouse() {
    const valid = this.data.houseId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择房屋信息!')
    // 返回验证结果
    return valid
  },
  verifyRepair() {
    const valid = this.data.repairItemId !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择维修项目!')
    // 返回验证结果
    return valid
  },
  verifyMobile() {
    // 验证手机号，记得给 mobile 一个默认值为 ''
    const reg = /^[1][3-8][0-9]{9}$/
    const valid = reg.test(this.data.mobile.trim())
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写正确的手机号码!')
    // 返回验证结果
    return valid
  },
  verifyDate() {
    // 验证日期格式
    const reg = /^\d{4}\-\d{2}\-\d{2}$/
    const valid = reg.test(this.data.appointment)
    // 验证结果提示
    if (!valid) wx.utils.toast('请选择预约日期!')
    // 返回验证结果
    return valid
  },
  verifyDescription() {
    // 验证报修项目描述，记得给 description 一个默认值为 ''
    const valid = this.data.description.trim() !== ''
    // 验证结果提示
    if (!valid) wx.utils.toast('请填写问题描述!')
    // 返回验证结果
    return valid
  },
  async submitForm() {
    if (!this.verifyHouse()) return
    if (!this.verifyRepair()) return
    if (!this.verifyMobile()) return
    if (!this.verifyDate()) return
    if (!this.verifyDescription()) return
    const {
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment,
      id
    } = this.data
    const {
      code,
    } = await wx.http.post('/repair', {
      id, // 后端会根据有没有 id 来自动区分是修改还是新增
      houseId,
      repairItemId,
      mobile,
      appointment,
      description,
      attachment
    })
    if (code !== 10000) return wx.utils.toast((this.data.id ? '编辑' : '添加') + '报修失败')

    // redirectTo 不会保留当前页面的历史记录，返回的时候不会到这个页面
    wx.redirectTo({
      url: '/repair_pkg/pages/list/index',
    })
  }
})